﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class create_endpoint : BaseForm
    {
        public create_endpoint()
            : base()
        {
        }

        public create_endpoint(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbAuthorization.DataSource = Utility.authorizations(dbContext);
            cbAuthorization.DisplayMember = "Name";

            cbCertificate.DataSource = Utility.master_certificates(dbContext);
            cbCertificate.DisplayMember = "Name";
        }

        private string Auth()
        {
            if (rbNTLM.Checked)
            {
                return " NTLM";
            }

            if (rbKerberos.Checked)
            {
                return " Kerberos";
            }

            if (rbNegotiate.Checked)
            {
                return " Negotiate";
            }

            return string.Empty;
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CREATE ENDPOINT [" + tbName.Text + "]");
            if (chAuthorization.Checked)
            {
                sb.AppendLine("AUTHORIZATION [" + cbAuthorization.Text + "]");
            }

            if (rbStarted.Checked)
            {
                sb.AppendLine("STATE = STARTED");
            }
            else if (rbStopped.Checked)
            {
                sb.AppendLine("STATE = STOPPED");
            }
            else if (rbDisabled.Checked)
            {
                sb.AppendLine("STATE = DISABLED");
            }

            sb.AppendLine("AS TCP");
            sb.AppendLine("(");

            sb.AppendLine("   LISTENER_PORT = " + tbPort.Text);

            if (!cbAll.Checked) sb.AppendLine("  ,LISTENER_IP = " + tbIP.Text);
            else sb.AppendLine("  ,LISTENER_IP = ALL");

            sb.AppendLine(")");

            sb.AppendLine("FOR SERVICE_BROKER ");
            sb.AppendLine("(");

            bool auth = true;

            if (chFirst.Checked)
            {
                sb.Append("   AUTHENTICATION = ");
                sb.Append("      WINDOWS " + Auth());
                sb.Append(" CERTIFICATE [" + cbCertificate.Text + "]");

                sb.AppendLine();
            }
            else
            {
                if (cbWindows.Checked)
                {
                    if (chUseCertificate.Checked)
                    {
                        sb.Append("   AUTHENTICATION = CERTIFICATE [" + cbCertificate.Text + "]");
                        sb.Append(" WINDOWS " + Auth());
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.AppendLine("   AUTHENTICATION = WINDOWS " + Auth());
                    }
                }
                else
                {
                    if (chUseCertificate.Checked)
                    {
                        sb.AppendLine("   AUTHENTICATION = CERTIFICATE [" + cbCertificate.Text + "]");
                    }
                    else auth = false;
                }
            }

            bool encrypt = false;

            if (chEncrypt.Checked)
            {
                encrypt = true;

                if (auth)
                {
                    sb.Append("  ,");
                }
                else sb.Append("   ");

                sb.Append("ENCRYPTION = ");

                if (rbSupported.Checked) sb.Append(" SUPPORTED ");
                else sb.Append(" REQUIRED ");

                if (rbRC4.Checked) sb.Append(" ALGORITHM RC4");

                if (rbRC4Aes.Checked) sb.Append(" ALGORITHM RC4 AES ");

                if (rbAES.Checked) sb.Append(" ALGORITHM AES ");

                if (rbAesRC4.Checked) sb.Append(" ALGORITHM AES RC4");

                sb.AppendLine();
            }
            else
            {
                encrypt = true;

                if (auth)
                {
                    sb.Append("  ,");
                }
                else sb.Append("   ");

                sb.AppendLine("ENCRYPTION = DISABLED");
            }

            if (chForwarding.Checked)
            {
                if (auth || encrypt)
                    sb.Append("  ,");
                else
                    sb.Append("   ");

                sb.Append("MESSAGE_FORWARDING = ENABLED, ");
                sb.Append("MESSAGE_FORWARDING_SIZE = " + tbForwardSize.Text);
                sb.AppendLine();
            }
            else
            {
                if (auth || encrypt)
                    sb.AppendLine("  ,MESSAGE_FORWARDING = DISABLED");
                else
                    sb.AppendLine("   MESSAGE_FORWARDING = DISABLED");
            }

            sb.AppendLine(")");

            tbCommand.Text = sb.ToString();

        }

        private void EnableListenerIP()
        {
            tbIP.Enabled = false;
        }

        private void DisableListenerIP()
        {
            tbIP.Enabled = true;
        }

        private void EnableEncryption()
        {
            gbEncrypt.Enabled = true;
            rbSupported.Enabled = true;
            rbRequired.Enabled = true;

            gbAlgor.Enabled = true;
            rbRC4.Enabled = true;
            rbAES.Enabled = true;
            rbAesRC4.Enabled = true;
            rbRC4Aes.Enabled = true;
        }

        private void DisableEncryption()
        {
            gbEncrypt.Enabled = false;
            rbSupported.Enabled = false;
            rbRequired.Enabled = false;

            gbAlgor.Enabled = false;
            rbRC4.Enabled = false;
            rbAES.Enabled = false;
            rbAesRC4.Enabled = false;
            rbRC4Aes.Enabled = false;
        }

        private void EnableWindowsAuth()
        {
            gbWindows.Enabled = true;

            rbNegotiate.Enabled = true;
            rbKerberos.Enabled = true;
            rbNTLM.Enabled = true;
        }

        private void DisableWindowsAuth()
        {
            gbWindows.Enabled = false;

            rbNegotiate.Enabled = false;
            rbKerberos.Enabled = false;
            rbNTLM.Enabled = false;
        }

        private void EnableCertificate()
        {
            lblCertificate.Enabled = true;
            cbCertificate.Enabled = true;
            cbCertificate.SelectedIndex = -1;
        }

        private void DisableCertificate()
        {
            lblCertificate.Enabled = false;
            cbCertificate.Enabled = false;
        }

        private void EnableForwarding()
        {
            lbForwardSize.Enabled = true;
            tbForwardSize.Enabled = true;
        }

        private void DisableForwarding()
        {
            lbForwardSize.Enabled = false;
            tbForwardSize.Enabled = false;
        }

        private void EnabelAuthorization()
        {
            cbAuthorization.Enabled = true;
        }

        private void DisableAuthorization()
        {
            cbAuthorization.Enabled = false;
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chAuthorization_CheckedChanged(object sender, EventArgs e)
        {
            if (chAuthorization.Checked) EnabelAuthorization();
            else DisableAuthorization();

            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbStarted_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbStopped_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbDisabled_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void tbPort_TextChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAll.Checked) EnableListenerIP();
            else DisableListenerIP();

            Write();
        }

        private void tbIP_TextChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void cbWindows_CheckedChanged(object sender, EventArgs e)
        {
            if (cbWindows.Checked)
            {
                EnableWindowsAuth();

                if (chUseCertificate.Checked) chFirst.Enabled = true;
                else chFirst.Enabled = false;
            }
            else
            {
                DisableWindowsAuth();
                chFirst.Enabled = false;
            }

            Write();
        }

        private void rbNTLM_CheckedChanged(object sender, EventArgs e)
        {
            Write();

        }

        private void rbKerberos_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbNegotiate_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void cbCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void chUseCertificate_CheckedChanged(object sender, EventArgs e)
        {
            if (chUseCertificate.Checked)
            {
                EnableCertificate();

                if (cbWindows.Checked) chFirst.Enabled = true;
                else chFirst.Checked = false;
            }
            else
            {
                DisableCertificate();
                chFirst.Enabled = false;
            }

            Write();
        }

        private void chFirst_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void chEncrypt_CheckedChanged(object sender, EventArgs e)
        {
            if (chEncrypt.Checked) EnableEncryption();
            else DisableEncryption();

            Write();
        }

        private void rbSupported_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbRequired_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbRC4_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbAES_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbAesRC4_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void rbRC4Aes_CheckedChanged(object sender, EventArgs e)
        {

            Write();
        }

        private void chForwarding_CheckedChanged(object sender, EventArgs e)
        {
            if (chForwarding.Checked)
            {
                EnableForwarding();
            }
            else DisableForwarding();

            Write();
        }

        private void tbForwardSize_TextChanged(object sender, EventArgs e)
        {

            Write();
        }
    }
}